import Image
import os
import csv
import collections
from keras.preprocessing.image import *
import matplotlib.pyplot as plt
from skimage.transform import rotate
import cv2

def read_iamge_info():
    src_folder = '/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/JD/noise_images/train'
    res_folder = '/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/JD/noise_images/train_trainsformed'
    for img in os.listdir(src_folder):
        path = src_folder + '/' + img
        im = Image.open(path)
        print im.mode
        im_arr = np.asarray(im).transpose(2,0,1)
        im_arr = im_arr[0:3][:][:]
        im_arr = im_arr.transpose(1,2,0)
        print im_arr.shape
        #im_arr = im_arr.transpose(2,0,1)
        im_save = Image.fromarray(im_arr)
        #im_arr = im_arr.transpose(0,1,2)
        #print im_arr
        print im_arr.shape
        path = res_folder + '/' + img
        im_save.save(path)

def convert_CMYK_to_RGB():
    src_folder = '/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/JD/noise_images/train'
    res_folder = '/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/JD/noise_images/train_trainsformed'
    for img in os.listdir(src_folder):
        path = src_folder + '/' + img
        im = Image.open(path)
        if im.mode == 'CMYK':
            print 'no'
        print im.mode
        im_save = im.convert('RGB')
        print im_save.mode
        im_arr = np.asarray(im_save).transpose(2,0,1)
        print im_arr.shape
        path = res_folder + '/' + img
        im_save.save(path)

def check_testset_order():
    testset_names = '/home/dell/wxm/Code/JD/testset_names.csv'
    test_names = '/home/dell/wxm/Code/JD/log_records/submit/baseline/64/test_names.csv'
    names1 = []
    names2 = []
    flag1 = 0
    flag2 = 0
    with open(testset_names) as csvfile1:
        reader1 = csv.reader(csvfile1)
        for row1 in reader1:
            names1.append(row1)
            flag1 += 1

    with open(test_names) as csvfile2:
        reader2 = csv.reader(csvfile2)
        for row2 in reader2:
            names2.append(row2)
            flag2 += 1
    wrong = 0

    if flag1 == flag2:
        print 'same length'
        for i in range(flag2):
            print str(names1[i]) + "  " + str(names2[i])
            if str(names1[i]) != str(names2[i]):
                wrong += 1
                print 'wrong: ' + str(wrong) + '\r' ,
    print 'wrong: ' + str(wrong)

def class_counter():
    file_path = '/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/JD/washed_train_label.csv'
    f = open(file_path,'rb')
    classes = []
    for line in f:
        line = line.strip('\n')
        name, c = line.split(',')
        if c == 6:
            print 'find'
        classes.append(c.replace('\r', ''))

    counter = dict(collections.Counter(classes))

    print counter
    for label in range(40):
        if label != 6 and label != 8:
            print "label " + str(label) + ' ' + str(counter[str(label)])
#        elif label == 6 or label == 8:
#            print 'exit'

def check_augmentation_skimage():
    im_arrs_raw = []
    im_arrs = []
    src_folder = '/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/JD/augmentation_test/raw'
    for img in os.listdir(src_folder):
        path = src_folder + '/' + img
        im = Image.open(path)
        im_arr = np.asarray(im)

        im_arr = im_arr.transpose(2,0,1)
        im_arrs_raw.append(im_arr)

        im_arr = rotate(im_arr,angle=300)
        im_arrs.append(im_arr)

    im_arrs = np.asarray(im_arrs)
    im_arrs_raw = np.asarray(im_arrs_raw)

    for i in range(im_arrs.shape[0]):
        plt.subplot(1,2,1)
        plt.imshow(im_arrs_raw[i].transpose(1,2,0))
        plt.subplot(1,2,2)
        plt.imshow(im_arrs[i].transpose(1,2,0))
        plt.show()

def check_augmentation_keras():
    im_arrs_raw = []
    im_arrs = []
    src_folder = '/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/JD/augmentation_test/raw'
    for img in os.listdir(src_folder):
        path = src_folder + '/' + img
        im = Image.open(path)
        im_arr = np.asarray(im)

        im_arrs_raw.append(im_arr)

        im_arr = np.rot90(im_arr,k=2)
        im_arrs.append(im_arr)

    im_arrs = np.asarray(im_arrs)
    im_arrs_raw = np.asarray(im_arrs_raw)

    for i in range(im_arrs.shape[0]):
        plt.subplot(1,2,1)
        plt.imshow(im_arrs_raw[i])
        plt.subplot(1,2,2)
        plt.imshow(im_arrs[i])
        plt.show()

def check_img_array():
    img_path = '/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/JD/testSample/Test060493.jpg'
    im = cv2.imread(img_path)
    white_flag = 0
    for i in range(224):
        if list(im[i,i,:]) == [255, 255, 255]:
            white_flag += 1
    print white_flag

def check_train_size():
    img_folder = '/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/JD/train'
    img_flag = 0
    noise_flag = 0

    for img in os.listdir(img_folder):
        img_flag += 1
        path = img_folder + '/' + img
        im = Image.open(path)
        im_arr = np.asarray(im)
        if im_arr.shape != (256,256,3):
            noise_flag += 1
            print str(im.mode) + ' :  ' + str(im_arr.shape)

        print 'img_flag: ' + str(img_flag) + ' | ',
        print "noise_flag: " + str(noise_flag) + '\r' ,

def test_wrap():
    img_path = '/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/JD/UFS_test/Test018415.jpg'

    im = Image.open(img_path)
    wrap_size = min(im.size)
    max_size = max(im.size)

    crop_point = (max_size - wrap_size)/2
    if max_size == im.size[0]:
        box = (crop_point, 0, crop_point+wrap_size, 0+wrap_size)
    else:
        box = (0, crop_point, 0+wrap_size, crop_point+wrap_size )

    im = im.crop(box)
    im.save('/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/JD/test_wrap.jpg')


def openBlas():
    file_path = '/home/dell/OpenBLAS/numpy/site.cfg'
    file_save = '/home/dell/error-sovle/site.cfg'
    f = open(file_path,'r')
    f_save = open(file_save,'w')
    lines = f.readlines()

    for line in os.listdir(lines):
        line = line.strip('\n')
        f_save.write(line)

def test_rand():
    labels = []
    for i in range(20597):
        labels.append(np.random.randint(0,40))
    counter = dict(collections.Counter(labels))
    print counter

def test_W_size():
    img_folder = '/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/JD/Proc/test_washed_FS_W'
    im_sizes = []
    for img in os.listdir(img_folder):
        path = img_folder + '/' + img
        im = Image.open(path)
        im_sizes.append(im.size)
    counter = dict(collections.Counter(im_sizes))
    print counter

def test_name_96():
    f1_path = '/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/JD/smallDataSet/one/train224/submit/test_names_all_96.csv'
    f2_path = '/home/dell/wxm/Code/JD/answer/aug_all_96.csv'

    f1= open(f1_path,'rb')
    f2 = open(f2_path,'rb')

    lines1 = f1.readlines()
    lines2 = f2.readlines()

    flag = 0

    for i in range(len(lines1)):
        name1 = lines1[i]
        name2, label = lines2[i].split(',')

        print name1 + '  ' + name2

        if str(name1) != str(name2):
            flag += 1
            #print name1 + ' | ' + name2
    print flag

if __name__ == '__main__':
    test_name_96()